package com.epcs.controller.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.epcs.model.bo.EventBO;
import com.epcs.model.dao.googleWrapper.GoogleAPIWrapper;
import com.epcs.model.dao.outlookWrapper.OutlookAPIWrapper;
import com.epcs.model.bo.EventcategoryBO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Eventcategory;
import com.epcs.model.dto.FullCalendarEvent;
import com.epcs.util.EpcsDateTimeUtil;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class MainAction extends ActionSupport implements SessionAware, ServletRequestAware {

	// ---------- input fields ----------//
	private SessionMap<String, Object> sessionMap;
	private HttpServletRequest request;
	private String redirectURL;

	// ----------Action method -----------//
	// ----- Get index page -----//
	public String getIndex() {
		String result = "Success";
		// get user session
		Account account = (Account) sessionMap.get("USER_INFO");
		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			boolean next = true;
			
			// với lần đầu tiên login thì lấy username (tức email) để get permission
			if (sessionMap.get("EMAIL_PERMISSION") != null){
				String email = (String)sessionMap.get("EMAIL_PERMISSION");
				
				// luu email session (để sau này còn lấy ra mà ghi vào DB)
				sessionMap.put("EMAIL", email);

				if (email.endsWith("@gmail.com")) {
					// set type of email
					sessionMap.put("EMAIL_TYPE", "GOOGLE");
					// set redirectURL
					redirectURL = GoogleAPIWrapper.getAuthURL();
					// redirect to permission page of google
					result = "GetEmailPermission";
					next = false;
										
				} else if (email.endsWith("@outlook.com") || email.endsWith("@hotmail.com")
						|| email.endsWith("@live.com")) {
					// set type of email
					sessionMap.put("EMAIL_TYPE", "OUTLOOK");
					// set redirectURL
					redirectURL = OutlookAPIWrapper.getAuthURL();
					// redirect to permission page of outlook
					result = "GetEmailPermission";
					next = false;
				} else {
					request.setAttribute("WARNING_MESSAGE", "Email bạn đăng ký không được hỗ trợ đồng bộ! Xin hãy vào Thiết lập để xác nhận email đồng bộ khác!");
					next = true;
				}
				
				sessionMap.remove("EMAIL_PERMISSION");
			}
			
			if (next){
				// lay thong tin du lieu can thiet tren index page

				String start = EpcsDateTimeUtil.getCurrentYearString() + "-01-01T00:00:00+0700";
				String end = (EpcsDateTimeUtil.getCurrentYearInteger() + 1) + "-01-01T00:00:00+0700";

				Gson gson = new Gson();
				EventBO eventbo = new EventBO();
				List<FullCalendarEvent> list = eventbo.getFullCalendarEventList(start, end, account);
				String resultstring = gson.toJson(list);
			
			EventcategoryBO newCatBO = new EventcategoryBO();
			List<Eventcategory> catList = newCatBO.getAllEventCategory(false);
			request.setAttribute("CATEGORY_LIST", catList);

				request.setAttribute("EVENT_LIST", resultstring);
			}
			
		} else {
			// neu chua login thi login
			return "Unauthorized";
		}

		return result;
	}

	// -----------Getter and Setter -----//

	public String getRedirectURL() {
		return redirectURL;
	}

	public void setRedirectURL(String redirectURL) {
		this.redirectURL = redirectURL;
	}
	
	// ---------------- session --------------------//
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = (SessionMap<String, Object>) sessionMap;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}


}
